package com.du.cloud.security.component;

import com.du.cloud.common.base.constant.enums.ResultCode;
import com.du.cloud.security.exception.CustomAuth2Exception;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import lombok.SneakyThrows;


/**
 * Desc: Oauth2 异常格式化
 * Author: dushuang
 * Date: 2020/5/26
 **/
public class CustomAuth2ExceptionSerializer extends StdSerializer<CustomAuth2Exception> {

    public CustomAuth2ExceptionSerializer() {
        super(CustomAuth2Exception.class);
    }

    @Override
    @SneakyThrows
    public void serialize(CustomAuth2Exception e, JsonGenerator gen, SerializerProvider provider) {
        gen.writeStartObject();
        gen.writeObjectField("code", ResultCode.FAILED.code);
        gen.writeObjectField("msg", e.getMessage());
        gen.writeObjectField("data", e.getErrorCode());
        gen.writeEndObject();
    }
}
